package com.tiantianyuegou.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.tiantianyuegou.R;
import com.tiantianyuegou.util.SelfUtils;
import com.tiantianyuegou.util.Tools;

/**
 * 设置密码
 */
public class SettingPwdActivity extends BaseActivity implements View.OnClickListener,TextWatcher {
    private EditText edt_setpwd, edt_setpwd_again,edt_setpaypwd,edt_setpaypwd_again,edt_register_pelese_account;
    private Button btn_setpwd_next;
    private String TAG = "SettingPwdActivity";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_pwd);
        Tools.changeSystemBar(this);
        setTitle("设置密码");
        initViews();
    }

    @Override
    public void findViewByIds() {
        edt_setpwd = (EditText) findViewById(R.id.edt_setpwd);
        edt_setpwd_again = (EditText) findViewById(R.id.edt_setpwd_again);
        edt_setpaypwd = (EditText) findViewById(R.id.edt_setpaypwd);
        edt_setpaypwd_again = (EditText) findViewById(R.id.edt_setpaypwd_again);
        btn_setpwd_next = (Button) findViewById(R.id.btn_setpwd_next);
        edt_register_pelese_account =(EditText)findViewById(R.id.edt_register_pelese_account);

    }

    @Override
    public void initListener() {
        edt_setpwd.addTextChangedListener(this);
        edt_setpwd_again.addTextChangedListener(this);
        edt_setpaypwd.addTextChangedListener(this);
        edt_setpaypwd_again.addTextChangedListener(this);
        btn_setpwd_next.setOnClickListener(this);

    }

    @Override
    public void initData() {

    }
    public void launchActivity() {
        String pwd = edt_setpwd.getText().toString().trim();
        String confirmPwd = edt_setpwd_again.getText().toString().trim();
        String paypwd = edt_setpaypwd.getText().toString().trim();
        String conpaypwd = edt_setpaypwd_again.getText().toString().trim();
        if (TextUtils.isEmpty(pwd)) {
            showMsg("请输入密码");
            return;
        }
        if(SelfUtils.isConSpeCharacters(pwd)){
            showMsg("密码中不能包含特殊字符");
            return;
        }
        if (pwd.length() < 6 || pwd.length() > 15) {
            showMsg("请输入6-15位长度的密码");
            return;
        }
        if(SelfUtils.isConSpeCharacters(confirmPwd)){
            showMsg("确认密码中不能包含特殊字符");
            return;
        }
        if (confirmPwd.length() < 6 || pwd.length() > 15) {
            showMsg("请输入6-15位长度的密码");
            return;
        }
        if (TextUtils.isEmpty(confirmPwd)) {
            showMsg("请再次输入密码");
            return;
        }
        if (!pwd.equals(confirmPwd)) {
            showMsg("两次登陆输入的密码不同，请重新输入");
            return;
        }
        if (TextUtils.isEmpty(paypwd)){
            showMsg("请输入支付密码");
            return;
        }
        if(SelfUtils.isConSpeCharacters(paypwd)){
            showMsg("密码中不能包含特殊字符");
            return;
        }
        if (paypwd.length() < 6 || paypwd.length() > 15) {
            showMsg("请输入6-15位长度的密码");
            return;
        }
        if(SelfUtils.isConSpeCharacters(conpaypwd)){
            showMsg("确认密码中不能包含特殊字符");
            return;
        }
        if (conpaypwd.length() < 6 || paypwd.length() > 15) {
            showMsg("请输入6-15位长度的密码");
            return;
        }
        if (TextUtils.isEmpty(conpaypwd)) {
            showMsg("请再次输入支付密码");
            return;
        }
        if (!paypwd.equals(conpaypwd)) {
            showMsg("两次支付输入的密码不同，请重新输入");
            return;
        }
        Intent intent = getIntent();
        String phone = intent.getStringExtra("phone");
        String vCode = intent.getStringExtra("code");
        String account = intent.getStringExtra("account");
//      TODO  联网获取真正的验证码
//        String paypwd = intent.getStringExtra("paypwd");
        Log.e(TAG, phone + "," + vCode);

        Intent setPwdNext = new Intent();
        setPwdNext.putExtra("password", pwd);
        setPwdNext.putExtra("phone", phone);
        setPwdNext.putExtra("vcode", vCode);
        setPwdNext.putExtra("account",account);
        setPwdNext.putExtra("paypassword", paypwd);
//        TODO 携带真正的验证码
        setPwdNext.putExtra("paypwd",paypwd);
        Log.e(TAG, "跳转跳转跳转"+pwd+phone+vCode);
        setPwdNext.setClass(SettingPwdActivity.this, UserInfoActivity.class);
        startActivity(setPwdNext);
        finish();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_setpwd_next:
                launchActivity();

                break;
            default:
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        String pwd = edt_setpwd.getText().toString();
        String pwd_confirm = edt_setpwd_again.getText().toString();
        if (pwd != null && !"".equals(pwd) && pwd_confirm != null && !"".equals(pwd_confirm)) {
            btn_setpwd_next.setEnabled(true);
            btn_setpwd_next.setAlpha(1.0f);
        } else {
            btn_setpwd_next.setEnabled(false);
            btn_setpwd_next.setAlpha(0.5f);
        }

    }
}
